// 简单的筛选功能测试
// 注意：这是一个简化的测试，实际项目中应该使用 Jest 或其他测试框架

const mockTasks = [
  {
    id: '1',
    title: '紧急会议',
    description: '与客户讨论项目进展',
    dueDate: '2025-08-28',
    priority: 'P0',
    category: 'work',
    tags: ['会议', '客户'],
    completed: false,
    createdAt: '2025-08-27T10:00:00Z',
    updatedAt: '2025-08-27T10:00:00Z',
    subtasks: []
  },
  {
    id: '2',
    title: '学习React',
    description: '完成React教程',
    dueDate: '2025-08-30',
    priority: 'P2',
    category: 'learning',
    tags: ['编程', 'React'],
    completed: false,
    createdAt: '2025-08-26T09:00:00Z',
    updatedAt: '2025-08-26T09:00:00Z',
    subtasks: []
  },
  {
    id: '3',
    title: '健身计划',
    description: '每天跑步30分钟',
    dueDate: null,
    priority: 'P3',
    category: 'health',
    tags: ['健身'],
    completed: true,
    createdAt: '2025-08-25T08:00:00Z',
    updatedAt: '2025-08-27T18:00:00Z',
    subtasks: []
  }
]

// 简化的筛选函数
function filterTasks(tasks, filters) {
  return tasks.filter(task => {
    // 优先级筛选
    if (filters.priorities.length > 0 && !filters.priorities.includes(task.priority)) {
      return false
    }

    // 分类筛选
    if (filters.categories.length > 0 && !filters.categories.includes(task.category)) {
      return false
    }

    // 状态筛选
    if (filters.statuses.includes('completed') && !task.completed) {
      return false
    }
    if (filters.statuses.includes('pending') && task.completed) {
      return false
    }

    return true
  })
}

// 简化的排序函数
function sortTasks(tasks, sortBy, direction = 'asc') {
  const sortedTasks = [...tasks].sort((a, b) => {
    let comparison = 0

    switch (sortBy) {
      case 'priority':
        const priorityOrder = { 'P0': 0, 'P1': 1, 'P2': 2, 'P3': 3 }
        comparison = priorityOrder[a.priority] - priorityOrder[b.priority]
        break
      case 'dueDate':
        if (!a.dueDate && !b.dueDate) comparison = 0
        else if (!a.dueDate) comparison = 1
        else if (!b.dueDate) comparison = -1
        else comparison = new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime()
        break
      case 'title':
        comparison = a.title.localeCompare(b.title, 'zh-CN')
        break
      default:
        comparison = 0
    }

    return direction === 'desc' ? -comparison : comparison
  })

  return sortedTasks
}

console.log('🧪 测试任务筛选和排序功能...')
console.log('')

// 测试数据
console.log('原始任务数据:')
mockTasks.forEach((task, index) => {
  console.log(`${index + 1}. ${task.title} - ${task.priority} - ${task.category} - ${task.completed ? '已完成' : '未完成'}`)
})
console.log('')

// 测试筛选
console.log('1. 筛选测试 - 只显示工作任务:')
const workTasks = filterTasks(mockTasks, {
  priorities: ['P0', 'P1', 'P2', 'P3'],
  categories: ['work'],
  statuses: ['pending', 'completed']
})
workTasks.forEach(task => {
  console.log(`   - ${task.title} (${task.category})`)
})
console.log('')

console.log('2. 筛选测试 - 只显示未完成任务:')
const pendingTasks = filterTasks(mockTasks, {
  priorities: ['P0', 'P1', 'P2', 'P3'],
  categories: ['work', 'learning', 'health'],
  statuses: ['pending']
})
pendingTasks.forEach(task => {
  console.log(`   - ${task.title} (${task.completed ? '已完成' : '未完成'})`)
})
console.log('')

console.log('3. 筛选测试 - 只显示高优先级任务 (P0, P1):')
const highPriorityTasks = filterTasks(mockTasks, {
  priorities: ['P0', 'P1'],
  categories: ['work', 'learning', 'health'],
  statuses: ['pending', 'completed']
})
highPriorityTasks.forEach(task => {
  console.log(`   - ${task.title} (${task.priority})`)
})
console.log('')

// 测试排序
console.log('4. 排序测试 - 按优先级排序:')
const sortedByPriority = sortTasks(mockTasks, 'priority', 'asc')
sortedByPriority.forEach(task => {
  console.log(`   - ${task.title} (${task.priority})`)
})
console.log('')

console.log('5. 排序测试 - 按标题排序:')
const sortedByTitle = sortTasks(mockTasks, 'title', 'asc')
sortedByTitle.forEach(task => {
  console.log(`   - ${task.title}`)
})
console.log('')

console.log('✅ 筛选和排序功能测试完成!')
console.log('现在可以在主页使用筛选按钮来筛选和排序任务了！')
